package com.shuoyue.ycgk.ui.mine.mok;

import android.content.Intent;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Mok;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.mok.MokAdapter;
import com.shuoyue.ycgk.ui.mok.MokDetailActivity;
import com.shuoyue.ycgk.ui.recruitment.job.JobContract;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的模考
 */
public class MyMokActivity extends BaseMvpActivity<MyMokContract.Presenter>
        implements MyMokContract.View {
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    MyMokAdapter mokAdapter;

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_sigle_list;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        pageTitle.setText("我的模考");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));

        mokAdapter = new MyMokAdapter(null);
        registEmptyView(mokAdapter, R.layout.layout_empty_mok);
        recyclerView.setAdapter(mokAdapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMyMok();
            refreshLayout.finishRefresh();
        });

        registEmptyView(mokAdapter);
        mokAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Mok jobInfo = (Mok) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_course:
                    startActivity(new Intent(mContext, CourseInfoActivity.class)
                            .putExtra("id", jobInfo.getRecommendProduct().getId()));
                    break;
                case R.id.report:
                    MokDetailActivity.start(mContext, jobInfo.getMockId());
                    break;
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new MyMokContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getMyMok();
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setMoks(List<Mok> num) {
        mokAdapter.setNewData(num);
    }
}
